#include "qnameproperty.h"

#include "../../libs/sharedLib/qnamevalidator.h"
#include "../../libs/sharedLib/qabstracthost.h"

#include <QStringList>

QNameProperty::QNameProperty(SHAREDLIB::QAbstractProperty *parent):
    QByteArrayProperty(parent)
{
    connect(this,SIGNAL(showValueChanged()),this,SLOT(value_changed()));
}

QWidget* QNameProperty::getEditor()
{
    if(attribute("editabled").toBool())
    {
        SHAREDLIB::QNameEdit *e=new SHAREDLIB::QNameEdit("[_a-zA-Z][_a-zA-Z0-9]{,64}");
        SHAREDLIB::QAbstractHost *host=(SHAREDLIB::QAbstractHost *)m_object->property("host").toInt();
        if(host!=NULL)
        {
            SHAREDLIB::QAbstractHost *p=host->getParent();
            if(p!=NULL)
            {
                QStringList list;
                QList<SHAREDLIB::QAbstractHost *> c=p->getChildren();
                c.removeAll(host);
                foreach(SHAREDLIB::QAbstractHost *t,c)
                {
                    list.append(t->getPropertyValue("objectName").toString());
                }
                e->setIntermediateList(list);
            }
        }
        connect(e,SIGNAL(textEdited(QString)),this,SLOT(text(QString)));
        m_editor=e;
    }
    return QAbstractProperty::getEditor();
}

void QNameProperty::text(const QString &)
{
    SHAREDLIB::QNameEdit * e=(SHAREDLIB::QNameEdit *)sender();
    QString str=e->value();
    if(str!=getValue())
    {
        setSignalValue(str);
    }
}

void QNameProperty::value_changed()
{
    if(m_editor!=NULL)
    {
        SHAREDLIB::QNameEdit * e=(SHAREDLIB::QNameEdit *)m_editor;
        e->setText(m_value.toString());
    }
}
